/**
 * 
 */
package com.iwords.api.web.action;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.iwords.constants.Constants;
import com.iwords.service.SysLibServiceLocal;
import com.iwords.service.object.SysLibEntity;
import com.iwords.utils.CodeUtils;
import com.iwords.api.web.base.BaseAction;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-13    
 * @description  系统课本-SERVLET        
 */
@WebServlet(name = "sysLibAction", urlPatterns = { "/sysLib" })
public class SysLibAction extends BaseAction<SysLibEntity> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	SysLibServiceLocal sysLibService;
	
 

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {

		String action = req.getParameter("action");

		if (Constants.SYSLIB_LIST_ACTION.equals(action)) {
			doFindSysLibList(req, res);

		}
		if(Constants.SYSLIB_SINGLE_ACTION.equals(action)){
			doFindSysLib(req,res);
		}

	}
	

	public void doFindSysLib(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {
		SysLibEntity entity = initEntity(req);
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		try {
			entity = sysLibService.findSysLibById(entity.getLibid());
			JSONObject defaultValue = new JSONObject();
			defaultValue.put("libid", entity.getLibid());
			defaultValue.put("description", CodeUtils.encode(entity.getDescription()));
			defaultValue.put("pid", entity.getPid());
			defaultValue.put("quantity", entity.getQuantity());
			defaultValue.put("sysliblevel", entity.getLiblevel());
			defaultValue.put("libname", CodeUtils.encode(entity.getLibname()));
			defaultValue.put("sortnum", entity.getSortnum());
			result.put(Constants.DEFAULT_VALUE, defaultValue);
		} catch (Exception e) {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, CodeUtils.encode("查询课本失败"));
		}
		jsonPrint(res, result);
	}

	public void doFindSysLibList(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		List<SysLibEntity> sysList = null;
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);

		try {
			sysList = sysLibService.findSysLibs();

			com.iwords.utils.JSONObject defaultValue = new com.iwords.utils.JSONObject();
			defaultValue.putColumn("libid", "description", "pid", "quantity", "sysliblevel", "libname", "sortnum");
			for (SysLibEntity sysLib : sysList) {
				defaultValue.putValue(sysLib.getLibid(), CodeUtils.encode(sysLib.getDescription()), sysLib.getPid(), sysLib.getQuantity(),
						sysLib.getLiblevel(), CodeUtils.encode(sysLib.getLibname()), sysLib.getSortnum());

			}
			result.put(Constants.DEFAULT_VALUE, defaultValue);
		} catch (Exception e) {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, CodeUtils.encode("查询课本列表失败"));
		}

		jsonPrint(res, result);
	}

	public SysLibEntity initEntity(HttpServletRequest req) {

		SysLibEntity sys = new SysLibEntity();
		if (req.getParameter("libid") != null) {
			sys.setLibid(Long.parseLong(req.getParameter("libid")));
		}
		return sys;

	}
}
